﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Automation;

namespace RPAHelper
{

    enum myssdfs
    {
      sdfs=0
    }

    public class RPAHelperForWPF
    {
 
        #region 单例
        private static RPAHelperForWPF _instance;

        public static RPAHelperForWPF Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new RPAHelperForWPF();

                return _instance;
            }
        }

        private RPAHelperForWPF()
        {

        }
        #endregion

        #region 公共方法
        //public string GetControlByName(string name)
        //{
        //    Playback.Initialize();

        //    WpfWindow w = new WpfWindow();
        //    w.SearchProperties[WpfWindow.PropertyNames.Name] = "Network Analyzer";
        //    w.SearchProperties.Add(new PropertyExpression(WpfWindow.PropertyNames.ClassName, "HwndWrapper", PropertyExpressionOperator.Contains));



        //    WpfCustom control = new WpfCustom(w);
        //    control.SearchProperties[WpfCustom.PropertyNames.Name] = name;

        //    //control.SearchProperties[WpfCustom.PropertyNames.BoundingRectangle] = name;

        //    WpfCustom control1 = new WpfCustom(control.GetParent());
        //    control1.SearchProperties[WpfCustom.PropertyNames.AutomationId] = "downLabel";


        //    ControlInfo info = new ControlInfo();
        //    info.Content = control1.Name;
        //    info.Left = control.GetParent().Left;
        //    info.Top = control.GetParent().Top;

        //    Playback.Cleanup();

        //    return info.ToString();
        //}

        //public ControlInfo GetDataGridCell(int rowIndex, string colName, bool IsEdit = false, string dgName = "AutoIdMDgdSwpSetSeg")
        //{


        //    Playback.Initialize();

        //    WpfWindow w = new WpfWindow();
        //    w.SearchProperties[WpfWindow.PropertyNames.Name] = "Network Analyzer";
        //    w.SearchProperties.Add(new PropertyExpression(WpfWindow.PropertyNames.ClassName, "HwndWrapper", PropertyExpressionOperator.Contains));

        //    WpfTable tb = new WpfTable(w);
        //    tb.SearchProperties[WpfTable.PropertyNames.AutomationId] = dgName;
        //    WpfRow row = tb.GetRow(rowIndex);

        //    WpfCell cell = new WpfCell(row);
        //    cell.SearchProperties[WpfCell.PropertyNames.ColumnHeader] = colName;

        //    string str = "";

        //    if (!IsEdit)
        //    {
        //        WpfText tx = new WpfText(cell);
        //        str = tx.Name;
        //    }
        //    else
        //    {
        //        WpfEdit edit = new WpfEdit(cell);
        //        str = edit.Text;
        //    }

        //    ControlInfo info = new ControlInfo();
        //    info.Content = str;
        //    info.Left = cell.Left;
        //    info.Top = cell.Top;

        //    Playback.Cleanup();

        //    return info;
        //}

        //public ControlInfo GetDataGridCell(int row, int col, bool IsEdit = false, string dgName = "AutoIdMDgdAnaLimLine")
        //{
        //    Playback.Initialize();

        //    WpfWindow w = new WpfWindow();
        //    w.SearchProperties[WpfWindow.PropertyNames.Name] = "Network Analyzer";
        //    w.SearchProperties.Add(new PropertyExpression(WpfWindow.PropertyNames.ClassName, "HwndWrapper", PropertyExpressionOperator.Contains));

        //    WpfTable tb = new WpfTable(w);
        //    tb.SearchProperties[WpfTable.PropertyNames.AutomationId] = dgName;


        //    WpfCell cell = tb.GetCell(row, col);

        //    string str = "";

        //    if (!IsEdit)
        //    {
        //        WpfText tx = new WpfText(cell);
        //        str = tx.Name;
        //    }
        //    else
        //    {
        //        WpfEdit edit = new WpfEdit(cell);
        //        str = edit.Text;
        //    }

        //    ControlInfo info = new ControlInfo();
        //    info.Content = str;
        //    info.Left = cell.Left;
        //    info.Top = cell.Top;

        //    Playback.Cleanup();

        //    return info;
        //}

        //public bool Test(string pic1, string pic2)
        //{
        //    Image actualImage = new Bitmap(pic1);
        //    Image expectedImage = new Bitmap(pic2);
        //    return ImageComparer.Compare(actualImage, expectedImage);
        //}

        public string GetControlByName1(string name)
        {
            AutomationElement aeTop = AutomationElement.RootElement;   //找到桌面根元素 
            AutomationElement aeForm = aeTop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Network Analyzer"));


            AutomationElement asControl = aeForm.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, name));

            var control = TreeWalker.ControlViewWalker.GetNextSibling(asControl);


            ControlInfo info = new ControlInfo();
            info.Content = control.Current.Name;
            info.Left = control.Current.BoundingRectangle.Left;
            info.Top = control.Current.BoundingRectangle.Top;
            return info.ToString();
        }


        public ControlInfo GetDataGridCell1(int rowIndex, string colName, bool IsEdit = false, string dgName = "AutoIdMDgdSwpSetSeg")
        {
            ControlInfo info = new ControlInfo();

            return info;
        }


        public ControlInfo GetDataGridCell1(int row, int col, bool IsEdit = false, string dgName = "AutoIdMDgdAnaLimLine")
        {
            AutomationElement aeTop = AutomationElement.RootElement;   //找到桌面根元素 
            AutomationElement aeForm = aeTop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Network Analyzer"));


            AutomationElement dataGrid = aeForm.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, dgName));

            //var rowControl = asControl.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "MDgRow" + row));
            //var control= rowControl.FindFirst(TreeScope.Children,)

            GridPattern pattern = dataGrid.GetCurrentPattern(GridPattern.Pattern) as GridPattern;
            AutomationElement cell = pattern.GetItem(row, col);

            var control = cell.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));


            ControlInfo info = new ControlInfo();
            info.Content = control.Current.Name;
            info.Left = control.Current.BoundingRectangle.Left;
            info.Top = control.Current.BoundingRectangle.Top;




            return info;
        }
        #endregion

    }


    public class ControlInfo
    {
        public string Content { get; set; }

        public double Top { get; set; }


        public double Left { get; set; }

        public override string ToString()
        {
            return "Content:" + Content + "-Top:" + Top + "-Left:" + Left;
        }
    }
}
